package com.yaodian100.ec.image.service.impl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.core.common.utils.ImageUtils;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.ec.image.entity.Image;
import com.yaodian100.ec.image.service.ImageService;
import com.yaodian100.ec.proposeproduct.service.ProposeProductService;

public class ImageServiceImpl extends DomainServiceImpl<Image> implements ImageService {
	private ProposeProductService proposeProductService;

	@Override
	@Transactional(readOnly = false)
	public void deletePic(String[] picIds) {
		try {
			List<Image> images = null;
			CommonCriteria criteria = null;
			File imageFile = null;
			for (int i = 0; i < picIds.length; i++) {
				Image image = getDao().get(Long.parseLong(picIds[i]));
				if (image.getType().equalsIgnoreCase("IMGB")) {
					criteria = new CommonCriteria();
					criteria.addRlike("imgFilePath", image.getImgFilePath().substring(0, image.getImgFilePath().length() - 5));
					images = getDao().getList(criteria, null);
					for (Image imagebs : images) {
						imagebs.setIsDelete("Y");
						image = save(imagebs);
						imageFile = new File(ServerValue.getMidApachePath() + image.getImgFilePath());
						imageFile.delete();
					}
				} else {
					image.setIsDelete("Y");
					image = save(image);
					imageFile = new File(ServerValue.getMidApachePath() + image.getImgFilePath());
					imageFile.delete();
				}
			}

		} catch (Exception e) {
			logger.info("deletePic error:{}" + e);

		}

	}

	@Override
	@Transactional(readOnly = false)
	public String editImgDetail(Long imgId, File[] imageDes, String picDesc) throws CoreException {
		String msg = editImgDetail(imgId, imageDes, picDesc, "");
		return msg;
	}

	@Override
	@Transactional(readOnly = false)
	public String editImgDetail(Long imgId, File[] imageDes, String picDesc, String picShowSeq) throws CoreException {
		String msg = "";
		Image image = getDao().get(imgId);
		CommonCriteria criteria = null;
		List<Image> images = null;
		if (image.getType().equalsIgnoreCase("IMGB")) {
			criteria = new CommonCriteria();
			criteria.addRlike("imgFilePath", image.getImgFilePath().substring(0, image.getImgFilePath().length() - 5));
			images = getDao().getList(criteria, null);
			for (Image imagebs : images) {
				if (StringUtils.isNotBlank(picDesc)) {
					imagebs.setPicDesc(picDesc);
				}

				if (StringUtils.isNotBlank(picShowSeq)) {
					imagebs.setShowSeq(Integer.parseInt(picShowSeq));
				}
				getDao().update(imagebs);
			}
		} else {
			if (StringUtils.isNotBlank(picDesc)) {
				image.setPicDesc(picDesc);
			}

			if (StringUtils.isNotBlank(picShowSeq)) {
				image.setShowSeq(Integer.parseInt(picShowSeq));
			}
			getDao().update(image);
		}

		if (null != image) {
			File f = null;
			InputStream inputStream = null;
			OutputStream out = null;
			BufferedImage bufferedImage = null;
			StringBuffer imgMfilePath = new StringBuffer();
			InputStream is = null;
			OutputStream os = null;
			imgMfilePath.append(ServerValue.getMidApachePath()).append("PImg").append(File.separator);
			if (null != imageDes && imageDes.length > 0) {
				try {
					f = new File(imgMfilePath.toString() + imageDes[0].getName());
					inputStream = new FileInputStream(imageDes[0]);
					out = new FileOutputStream(f);
					byte buf[] = new byte[1024];
					int len;
					while ((len = inputStream.read(buf)) > 0) {
						out.write(buf, 0, len);
					}
					out.close();
					inputStream.close();

					bufferedImage = ImageIO.read(new File(imgMfilePath.toString() + imageDes[0].getName()));
				} catch (Exception e) {
					e.printStackTrace();
				}
				// resize
				// imageUtils.mainResize(productId, bufferedImage, "jpg", imgMfilePath.toString());
				boolean isPass = true;

				long limitFileSize = 0;
				long thisFileSize = f.length();
				int limitImgWidth = 0;
				int limitImgHeight = 0;
				int thisImgWidth = bufferedImage.getWidth();
				int thisImgHeight = bufferedImage.getHeight();

				String filePath = image.getImgFilePath();
				int width = 0;
				int height = 0;
				boolean bb = false;
				boolean pressImage = true;
				boolean mainFileSize = true;
				boolean mainImgSize = true;
				ImageUtils imageUtils = new ImageUtils();
				if ("MAIN".equalsIgnoreCase(image.getType())) {
					String mainFileSizeMsg = "";
					String mainImgSizeMsg = "";
					if (filePath.endsWith("_m50.jpg")) {
						width = ImageUtils.main50ImgWidth;
						height = ImageUtils.main50ImgLen;
						bb = true;
						pressImage = false;
						limitFileSize = 8 * 1024;
						limitImgWidth = 50;
						limitImgHeight = 50;
					} else if (filePath.endsWith("_m75.jpg")) {
						width = ImageUtils.main75ImgWidth;
						height = ImageUtils.main75ImgLen;
						bb = true;
						pressImage = false;
						limitFileSize = 10 * 1024;
						limitImgWidth = 75;
						limitImgHeight = 75;
					} else if (filePath.endsWith("_m100.jpg")) {
						width = ImageUtils.main100ImgWidth;
						height = ImageUtils.main100ImgLen;
						bb = true;
						pressImage = false;
						limitFileSize = 16 * 1024;
						limitImgWidth = 100;
						limitImgHeight = 100;
					} else if (filePath.endsWith("_m135.jpg")) {
						width = ImageUtils.main135ImgWidth;
						height = ImageUtils.main135ImgLen;
						bb = true;
						pressImage = false;
						limitFileSize = 30 * 1024;
						limitImgWidth = 135;
						limitImgHeight = 135;
					} else if (filePath.endsWith("_m250.jpg")) {
						width = ImageUtils.main250ImgWidth;
						height = ImageUtils.main250ImgLen;
						bb = true;
						limitFileSize = 40 * 1024;
						limitImgWidth = 250;
						limitImgHeight = 250;
					} else if (filePath.endsWith("_m500.jpg")) {
						width = ImageUtils.main500ImgWidth;
						height = ImageUtils.main500ImgLen;
						bb = true;
						limitFileSize = 100 * 1024;
						limitImgWidth = 500;
						limitImgHeight = 500;
					}
					// 主图替换检查
					if (thisFileSize > limitFileSize) {
						isPass = false;
						mainFileSize = false;
						mainFileSizeMsg = "档案太大〔大小： " + limitFileSize / 8 + " kb] ";
					}
					if (thisImgHeight != limitImgHeight || thisImgWidth != limitImgWidth) {
						isPass = false;
						mainImgSize = false;
						mainImgSizeMsg = "尺寸不符〔原始尺寸： " + thisImgHeight + " ｘ " + thisImgWidth + "〕 ";
					}
					if (mainFileSize == false || mainImgSize == false) {
						msg = image.getImgFilePath() + " : ";
						if (!mainFileSize) {
							msg = msg + mainFileSizeMsg;
						}
						if (!mainImgSize) {
							msg = msg + mainImgSizeMsg;
						}
					}
				} else if ("RMAIN".equalsIgnoreCase(image.getType())) {
					String mainFileSizeMsg = "";
					String mainImgSizeMsg = "";
					if (filePath.endsWith("_rm93.jpg")) {
						width = ImageUtils.rmain70ImgWidth;
						height = ImageUtils.rmain93ImgLen;
						bb = true;
						pressImage = false;
						limitFileSize = 8 * 1024;
						limitImgWidth = 70;
						limitImgHeight = 93;
					} else if (filePath.endsWith("_rm130.jpg")) {
						width = ImageUtils.rmain100ImgWidth;
						height = ImageUtils.rmain130ImgLen;
						bb = true;
						pressImage = false;
						limitFileSize = 10 * 1024;
						limitImgWidth = 100;
						limitImgHeight = 130;
					} else if (filePath.endsWith("_rm180.jpg")) {
						width = ImageUtils.rmain135ImgWidth;
						height = ImageUtils.rmain180ImgLen;
						bb = true;
						pressImage = false;
						limitFileSize = 16 * 1024;
						limitImgWidth = 135;
						limitImgHeight = 180;
					} else if (filePath.endsWith("_rm225.jpg")) {
						width = ImageUtils.rmain170ImgWidth;
						height = ImageUtils.rmain225ImgLen;
						bb = true;
						pressImage = false;
						limitFileSize = 30 * 1024;
						limitImgWidth = 170;
						limitImgHeight = 225;
					} else if (filePath.endsWith("_rm330.jpg")) {
						width = ImageUtils.rmain250ImgWidth;
						height = ImageUtils.rmain330ImgLen;
						bb = true;
						limitFileSize = 40 * 1024;
						limitImgWidth = 250;
						limitImgHeight = 330;
					} else if (filePath.endsWith("_rm530.jpg")) {
						width = ImageUtils.rmain400ImgWidth;
						height = ImageUtils.rmain530ImgLen;
						bb = true;
						limitFileSize = 100 * 1024;
						limitImgWidth = 400;
						limitImgHeight = 530;
					}
					// 主图替换检查
					if (thisFileSize > limitFileSize) {
						isPass = false;
						mainFileSize = false;
						mainFileSizeMsg = "档案太大〔大小： " + limitFileSize / 8 + " kb] ";
					}
					if (thisImgHeight != limitImgHeight || thisImgWidth != limitImgWidth) {
						isPass = false;
						mainImgSize = false;
						mainImgSizeMsg = "尺寸不符〔原始尺寸： " + thisImgHeight + " ｘ " + thisImgWidth + "〕 ";
					}
					if (mainFileSize == false || mainImgSize == false) {
						msg = image.getImgFilePath() + " : ";
						if (!mainFileSize) {
							msg = msg + mainFileSizeMsg;
						}
						if (!mainImgSize) {
							msg = msg + mainImgSizeMsg;
						}
					}
				} else if ("DESC".equalsIgnoreCase(image.getType())) {
					width = ImageUtils.descBigImgWidth;
					height = bufferedImage.getHeight();
					bb = false;
					pressImage = false;
					// } else if ("IMGS".equalsIgnoreCase(image.getType())) {
					// width = ImageUtils.productSmallWidth;
					// height = ImageUtils.productSmallLen;
					// bb = true;
				} else if ("IMGB".equalsIgnoreCase(image.getType())) {
					width = ImageUtils.productBigWidth;
					height = ImageUtils.productBigLen;
					bb = true;
				}
				if (isPass) {
					if (image.getType().equalsIgnoreCase("MAIN")) {
						// upload img file
						StringBuffer imgFilePath = new StringBuffer();
						imgFilePath.append(ServerValue.getMidApachePath()).append("PImg").append(File.separator);
						try {
							is = new FileInputStream(imageDes[0]);
							os = new FileOutputStream(imgFilePath.append(image.getProductId().substring(0, 4)).append(File.separator)
									.append(image.getProductId()).append(File.separator).append(image.getProductId()).append("_m")
									.append(height).append(".jpg").toString());
							byte buf[] = new byte[1024];
							int len;
							while ((len = is.read(buf)) > 0) {
								os.write(buf, 0, len);
							}
							os.close();
							is.close();
							imgFilePath.setLength(0);
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					} else if (image.getType().equalsIgnoreCase("RMAIN")) {
						// upload img file
						StringBuffer imgFilePath = new StringBuffer();
						imgFilePath.append(ServerValue.getMidApachePath()).append("PImg").append(File.separator);
						try {
							is = new FileInputStream(imageDes[0]);
							os = new FileOutputStream(imgFilePath.append(image.getProductId().substring(0, 4)).append(File.separator)
									.append(image.getProductId()).append(File.separator).append(image.getProductId()).append("_rm")
									.append(height).append(".jpg").toString());
							byte buf[] = new byte[1024];
							int len;
							while ((len = is.read(buf)) > 0) {
								os.write(buf, 0, len);
							}
							os.close();
							is.close();
							imgFilePath.setLength(0);
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					} else {
						if (image.getType().equalsIgnoreCase("IMGB")) {
							ImageUtils.customChange(ImageUtils.productBigWidth, ImageUtils.productBigLen, bb, bufferedImage,
									ServerValue.getMidApachePath()
											+ image.getImgFilePath().substring(1, image.getImgFilePath().length() - 5) + "b.jpg", pressImage);
							ImageUtils.customChange(ImageUtils.productSmallWidth, ImageUtils.productSmallLen, bb, bufferedImage,
									ServerValue.getMidApachePath()
											+ image.getImgFilePath().substring(1, image.getImgFilePath().length() - 5) + "s.jpg", pressImage);
						} else {
							ImageUtils.customChange(width, height, bb, bufferedImage, ServerValue.getMidApachePath()
									+ image.getImgFilePath().substring(1), pressImage);
						}
					}
				}
			}
		}
		return msg;

	}

	public ProposeProductService getProposeProductService() {
		return proposeProductService;
	}

	public void setProposeProductService(ProposeProductService proposeProductService) {
		this.proposeProductService = proposeProductService;
	}

	@Override
	@Transactional(readOnly = false)
	public boolean uploadDescImage(String productId, String type, File[] imageFiles, String[] imageName)
			throws CoreException {
		StringBuffer imgMfilePath = new StringBuffer();
		imgMfilePath.append(ServerValue.getMidApachePath()).append("PImg").append(File.separator);
		try {
			String baseImgPath = "/PImg/" + productId.substring(0, 4) + "/" + productId + "/" + productId;
			Image image = null;
			File f = null;
			InputStream inputStream = null;
			OutputStream out = null;
			BufferedImage bufferedImage = null;
			ImageUtils imageUtils = new ImageUtils();
			StringBuilder sbNameMapKey = new StringBuilder();
			StringBuilder sbNameMapValue = new StringBuilder();
			Map<String, String> replaceNameMap = new HashMap<String, String>();
			List<String> replaceMapKeyList = new ArrayList<String>();
			// 上传说明檔
			if (StringUtils.equalsIgnoreCase("DESC", type)) {
				for (int i = 0; i < imageFiles.length; i++) {
					String timeflag = String.valueOf(new Date().getTime());
					f = new File(imgMfilePath.toString() + imageFiles[i].getName());
					inputStream = new FileInputStream(imageFiles[i]);
					out = new FileOutputStream(f);
					byte buf[] = new byte[1024];
					int len;
					while ((len = inputStream.read(buf)) > 0) {
						out.write(buf, 0, len);
					}
					out.close();
					inputStream.close();
					bufferedImage = ImageIO.read(new File(imgMfilePath.toString() + imageFiles[i].getName()));
					imageUtils.descResize(productId, bufferedImage, "d" + timeflag, "jpg", imgMfilePath.toString());
					image = new Image();
					image.setImgFilePath(baseImgPath + "_d" + timeflag + "." + "jpg");
					image.setProductId(productId);
					image.setShowSeq(i + 1);
					image.setType("DESC");
					getDao().save(image);
					// 置换图档名
					sbNameMapKey.setLength(0);
					sbNameMapValue.setLength(0);
					sbNameMapKey.append(productId).append("_d").append(timeflag).append(".jpg");
					sbNameMapValue.append(imageName[i]).append(",").append(productId);
					replaceNameMap.put(sbNameMapKey.toString(), sbNameMapValue.toString());
					replaceMapKeyList.add(sbNameMapKey.toString());
				}
				// 開始置換HTML的圖檔名稱
				proposeProductService.replaceImageName(replaceNameMap, replaceMapKeyList);

				File fileDel = new File(ServerValue.getMidApachePath() + File.separator + "PImg" + File.separator);
				File[] files = fileDel.listFiles(new FilenameFilter() {

					public boolean accept(File dir, String name) {
						return name.length() != 4
								&& ("logo_ok_b.png,blank_75.gif,blank_500.gif,blank_50.gif,blank_250.gif,blank_135.gif,blank_100.gif,tmpZip,blank_r93.gif,blank_r130.gif,blank_r180.gif,blank_r330.gif,blank_r225.gif,blank_r530.gif,40x40.gif,blank_225.gif,blank_r99.gif"
										.indexOf(name) == -1);
					}
				});

				if (null != files) {
					for (int i = 0; i < files.length; i++) {
						FileUtils.forceDelete(files[i]);
					}
				}
			}
		} catch (Exception e) {
			logger.info("error:{}", e.getMessage());
			return false;
		}
		return true;
	}

	@Override
	@Transactional(readOnly = false)
	public boolean uploadImage(String productId, String type, File[] imageFiles, String[] imageName) throws CoreException {
		try {
			uploadImage(productId, type, imageFiles, imageName, null);
		} catch (Exception e) {
			logger.info("error:{}", e.getMessage());
			return false;
		}
		return true;
	}

	@Override
	@Transactional(readOnly = false)
	public boolean uploadImage(String productId, String type, File[] imageFiles, String[] imageName, String picDesc)
			throws CoreException {
		StringBuffer imgMfilePath = new StringBuffer();
		imgMfilePath.append(ServerValue.getMidApachePath()).append("PImg").append(File.separator);
		try {
			String baseImgPath = "/PImg/" + productId.substring(0, 4) + "/" + productId + "/" + productId;
			Image image = null;
			File f = null;
			InputStream inputStream = null;
			OutputStream out = null;
			BufferedImage bufferedImage = null;
			ImageUtils imageUtils = new ImageUtils();
			// 上传主檔
			if (StringUtils.equalsIgnoreCase("MAIN", type)) {
				// 先刪除已存在的主圖檔
				CommonCriteria crit = new CommonCriteria();
				crit.addEq("productId", productId);
				crit.addEq("type", "MAIN");
				List<Image> imagelist = getDao().getList(crit, null);
				if (imagelist.size() > 0) {
					for (Image img : imagelist) {
						getDao().delete(img);
					}
				}
				f = new File(imgMfilePath.toString() + imageFiles[0].getName());
				inputStream = new FileInputStream(imageFiles[0]);
				out = new FileOutputStream(f);
				byte buf[] = new byte[1024];
				int len;
				while ((len = inputStream.read(buf)) > 0) {
					out.write(buf, 0, len);
				}
				out.close();
				inputStream.close();
				bufferedImage = ImageIO.read(new File(imgMfilePath.toString() + imageFiles[0].getName()));
				// resize
				imageUtils.mainResize(productId, bufferedImage, "jpg", imgMfilePath.toString());

				image = new Image();
				image.setImgFilePath(baseImgPath + "_m500.jpg");
				image.setProductId(productId);
				image.setShowSeq(1);
				image.setType("MAIN");
				image.setPicDesc(picDesc);
				image.setIsDelete("N");
				getDao().save(image);

				image = new Image();
				image.setImgFilePath(baseImgPath + "_m250.jpg");
				image.setProductId(productId);
				image.setShowSeq(2);
				image.setType("MAIN");
				image.setPicDesc(picDesc);
				image.setIsDelete("N");
				getDao().save(image);

				image = new Image();
				image.setImgFilePath(baseImgPath + "_m135.jpg");
				image.setProductId(productId);
				image.setShowSeq(3);
				image.setType("MAIN");
				image.setPicDesc(picDesc);
				image.setIsDelete("N");
				getDao().save(image);

				image = new Image();
				image.setImgFilePath(baseImgPath + "_m100.jpg");
				image.setProductId(productId);
				image.setShowSeq(4);
				image.setType("MAIN");
				image.setPicDesc(picDesc);
				image.setIsDelete("N");
				getDao().save(image);

				image = new Image();
				image.setImgFilePath(baseImgPath + "_m75.jpg");
				image.setProductId(productId);
				image.setShowSeq(5);
				image.setType("MAIN");
				image.setPicDesc(picDesc);
				image.setIsDelete("N");
				getDao().save(image);

				image = new Image();
				image.setImgFilePath(baseImgPath + "_m50.jpg");
				image.setProductId(productId);
				image.setShowSeq(6);
				image.setType("MAIN");
				image.setPicDesc(picDesc);
				image.setIsDelete("N");
				getDao().save(image);

			}
			if (StringUtils.equalsIgnoreCase("RMAIN", type)) {
				// 先刪除已存在的主圖檔
				CommonCriteria crit = new CommonCriteria();
				crit.addEq("productId", productId);
				crit.addEq("type", "RMAIN");
				List<Image> imagelist = getDao().getList(crit, null);
				if (imagelist.size() > 0) {
					for (Image img : imagelist) {
						getDao().delete(img);
					}
				}
				f = new File(imgMfilePath.toString() + imageFiles[0].getName());
				inputStream = new FileInputStream(imageFiles[0]);
				out = new FileOutputStream(f);
				byte buf[] = new byte[1024];
				int len;
				while ((len = inputStream.read(buf)) > 0) {
					out.write(buf, 0, len);
				}
				out.close();
				inputStream.close();
				bufferedImage = ImageIO.read(new File(imgMfilePath.toString() + imageFiles[0].getName()));
				// resize
				imageUtils.rmainResize(productId, bufferedImage, "jpg", imgMfilePath.toString());

				image = new Image();
				image.setImgFilePath(baseImgPath + "_rm530.jpg");
				image.setProductId(productId);
				image.setShowSeq(1);
				image.setType("RMAIN");
				image.setPicDesc(picDesc);
				image.setIsDelete("N");
				getDao().save(image);

				image = new Image();
				image.setImgFilePath(baseImgPath + "_rm330.jpg");
				image.setProductId(productId);
				image.setShowSeq(2);
				image.setType("RMAIN");
				image.setPicDesc(picDesc);
				image.setIsDelete("N");
				getDao().save(image);

				image = new Image();
				image.setImgFilePath(baseImgPath + "_rm225.jpg");
				image.setProductId(productId);
				image.setShowSeq(3);
				image.setType("RMAIN");
				image.setPicDesc(picDesc);
				image.setIsDelete("N");
				getDao().save(image);

				image = new Image();
				image.setImgFilePath(baseImgPath + "_rm180.jpg");
				image.setProductId(productId);
				image.setShowSeq(4);
				image.setType("RMAIN");
				image.setPicDesc(picDesc);
				image.setIsDelete("N");
				getDao().save(image);

				image = new Image();
				image.setImgFilePath(baseImgPath + "_rm130.jpg");
				image.setProductId(productId);
				image.setShowSeq(5);
				image.setType("RMAIN");
				image.setPicDesc(picDesc);
				image.setIsDelete("N");
				getDao().save(image);

				image = new Image();
				image.setImgFilePath(baseImgPath + "_rm93.jpg");
				image.setProductId(productId);
				image.setShowSeq(6);
				image.setType("RMAIN");
				image.setPicDesc(picDesc);
				image.setIsDelete("N");
				getDao().save(image);

			}
			// 上传说明檔
			if (StringUtils.equalsIgnoreCase("DESC", type)) {
				StringBuilder sbNameMapKey = new StringBuilder();
				StringBuilder sbNameMapValue = new StringBuilder();
				Map<String, String> replaceNameMap = new HashMap<String, String>();
				List<String> replaceMapKeyList = new ArrayList<String>();
				for (int i = 0; i < imageFiles.length; i++) {
					// String timeflag = String.valueOf(new Date().getTime());
					String timeflag = StringUtils.leftPad(((BigDecimal) getDao().getSQLQueryByList(
							"select seq_image_name.nextval from dual", null, 0, -1).get(0)).toString(), 13, "0");
					f = new File(imgMfilePath.toString() + imageFiles[i].getName());
					inputStream = new FileInputStream(imageFiles[i]);
					out = new FileOutputStream(f);
					byte buf[] = new byte[1024];
					int len;
					while ((len = inputStream.read(buf)) > 0) {
						out.write(buf, 0, len);
					}
					out.close();
					inputStream.close();
					bufferedImage = ImageIO.read(new File(imgMfilePath.toString() + imageFiles[i].getName()));
					imageUtils.descResize(productId, bufferedImage, "d" + timeflag, "jpg", imgMfilePath.toString());
					image = new Image();
					image.setImgFilePath(baseImgPath + "_d" + timeflag + "." + "jpg");
					image.setProductId(productId);
					image.setShowSeq(i + 1);
					image.setType("DESC");
					image.setPicDesc(picDesc);
					image.setIsDelete("N");
					getDao().save(image);
					if (null != imageName) {
						// 置换图档名
						sbNameMapKey.setLength(0);
						sbNameMapValue.setLength(0);
						sbNameMapKey.append(productId).append("_d").append(timeflag).append(".jpg");
						sbNameMapValue.append(imageName[i]).append(",").append(productId);
						replaceNameMap.put(sbNameMapKey.toString(), sbNameMapValue.toString());
						replaceMapKeyList.add(sbNameMapKey.toString());
					}
				}
				// 開始置換HTML的圖檔名稱
				if (MapUtils.isNotEmpty(replaceNameMap)) {
					proposeProductService.replaceImageName(replaceNameMap, replaceMapKeyList);
				}
			}

			// 上传商品图
			if (StringUtils.equalsIgnoreCase("PRDT", type)) {
				for (int i = 0; i < imageFiles.length; i++) {
					// String timeflag = String.valueOf(new Date().getTime());
					String timeflag = StringUtils.leftPad(((BigDecimal) getDao().getSQLQueryByList(
							"select seq_image_name.nextval from dual", null, 0, -1).get(0)).toString(), 13, "0");
					f = new File(imgMfilePath.toString() + imageFiles[i].getName());
					inputStream = new FileInputStream(imageFiles[i]);
					out = new FileOutputStream(f);
					byte buf[] = new byte[1024];
					int len;
					while ((len = inputStream.read(buf)) > 0) {
						out.write(buf, 0, len);
					}
					out.close();
					inputStream.close();
					bufferedImage = ImageIO.read(new File(imgMfilePath.toString() + imageFiles[i].getName()));
					imageUtils.prdtResize(productId, bufferedImage, timeflag, "jpg", imgMfilePath.toString());
					image = new Image();
					image.setImgFilePath(baseImgPath + "_" + timeflag + "_b" + "." + "jpg");
					image.setProductId(productId);
					image.setShowSeq(i + 1);
					image.setType("IMGB");
					image.setPicDesc(picDesc);
					image.setIsDelete("N");
					getDao().save(image);
					image = new Image();
					image.setImgFilePath(baseImgPath + "_" + timeflag + "_s" + "." + "jpg");
					image.setProductId(productId);
					image.setShowSeq(i + 1 + 1);
					image.setType("IMGS");
					image.setPicDesc(picDesc);
					image.setIsDelete("N");
					getDao().save(image);
				}
			}
			// File fileDel = new File(ServerValue.getMidApachePath() + File.separator + "PImg" + File.separator);
			// File[] files = fileDel.listFiles(new FilenameFilter() {
			//
			// public boolean accept(File dir, String name) {
			// return name.length() != 4 && !name.contains("logo_ok_b.png") && !name.contains("blank_75.gif")
			// && !name.contains("blank_500.gif") && !name.contains("blank_50.gif") && !name.contains("blank_250.gif")
			// && !name.contains("blank_135.gif") && !name.contains("blank_100.gif");
			// }
			// });
			//
			// if (null != files) {
			// for (int i = 0; i < files.length; i++) {
			// FileUtils.forceDelete(files[i]);
			// }
			// }
		} catch (Exception e) {
			logger.info("error:{}", e.getMessage());
			return false;
		}
		return true;
	}
}